<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList(1)">
      <el-form-item>
        <el-date-picker v-model="dataForm.key1" type="date" value-format="yyyy-MM-dd" placeholder="请假开始日期从" clearable></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="dataForm.key2" type="date" value-format="yyyy-MM-dd" placeholder="请假开始日期到" clearable></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(1)">查询</el-button>
        <el-button v-if="isAuth('flowservice:leaverequest:request')" type="primary" @click="addOrUpdateHandle()">申请</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        prop="days"
        header-align="center"
        align="center"
        width="80"
        label="请假天数">
      </el-table-column>
      <el-table-column
        prop="startdate"
        header-align="center"
        align="center"
        width="120"
        label="开始日期">
      </el-table-column>
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        label="补充说明">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        width="80"
        label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small">完成</el-tag>
          <el-tag v-else-if="scope.row.status === 1" size="small">挂起</el-tag>
          <el-tag v-else-if="scope.row.status === 2" size="small">审核中</el-tag>
          <el-tag v-else size="small">取消了</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="createtime"
        header-align="center"
        align="center"
        width="200"
        label="申请时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status === 2 || scope.row.status === 1" type="text" size="small" @click="terminateHandle(scope.row.processinstanceid)">删除</el-button>
          <el-button v-if="scope.row.status === 2" type="text" size="small" @click="suspendHandle(scope.row.processinstanceid)">挂起</el-button>
          <el-button v-if="scope.row.status === 1" type="text" size="small" @click="activeHandle(scope.row.processinstanceid)">激活</el-button>
          <el-button v-if="scope.row.status === 2 || scope.row.status === 1" type="text" size="small" @click="bpmnViewHandle(scope.row.processinstanceid)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!-- 弹窗, 查看模型 -->
    <bpmn-view v-if="viewVisible" ref="bpmnView"></bpmn-view>
  </div>
</template>

<script>
  import AddOrUpdate from './leaverequest-add-or-update'
  import BpmnView from './flow-view'
  import Vue from 'vue'
  export default {
    data () {
      return {
        dataForm: {
          key1: '',
          key2: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        viewVisible: false
      }
    },
    components: {
      AddOrUpdate,
      BpmnView
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList (page) {
        this.dataListLoading = true
        if (page) this.pageIndex = page
        this.$http({
          url: this.$http.adornUrl('/flow/leaverequest/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'timefrom': this.dataForm.key1,
            'timeto': this.dataForm.key2
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            var ssss = data.data.totalCount
            var iiiii = data.data.list
            for (let index in iiiii) {
              this.$http({
                url: this.$http.adornUrl('/flow/process/status'),
                method: 'get',
                params: this.$http.adornParams({
                  'processinstanceid': iiiii[index].processinstanceid
                })
              }).then(({data}) => {
                if (data && data.code === 0) {
                  Vue.set(iiiii[index], 'status', data.data)
                } else {
                  iiiii[index].status = 7
                }
              })
            }
            this.totalPage = ssss
            this.dataList = iiiii
            console.log(this.dataList)
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 终止流程
      terminateHandle (id) {
        this.$http({
          url: this.$http.adornUrl('/flow/process/terminate'),
          method: 'get',
          params: this.$http.adornParams({
            'processinstanceid': id
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 挂起流程
      suspendHandle (id) {
        this.$http({
          url: this.$http.adornUrl('/flow/process/suspend'),
          method: 'get',
          params: this.$http.adornParams({
            'processinstanceid': id
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 激活流程
      activeHandle (id) {
        this.$http({
          url: this.$http.adornUrl('/flow/process/active'),
          method: 'get',
          params: this.$http.adornParams({
            'processinstanceid': id
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 查看流程图
      bpmnViewHandle (id) {
        this.viewVisible = true
        this.$nextTick(() => {
          this.$refs.bpmnView.init(id)
        })
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      }
    }
  }
</script>
